﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Sale_ViewSales : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.TextBoxStartDate.Text = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).ToString("yyyy-MM-dd");
            this.TextBoxEndDate.Text = DateTime.Today.ToString("yyyy-MM-dd");
            this.TextBoxFindCode.Text = "";
        }
        ShowReport();
        
    }

    void ShowReport()
    {
        DateTime startdate = DateTime.MinValue;
        DateTime enddate = DateTime.MinValue;
        string code = "";
        try
        {
            startdate = DateTime.Parse(this.TextBoxStartDate.Text);
        }
        catch
        {
            LabelError.Text = "开始时间不合法！";
            return;
        }
        try
        {
            enddate = DateTime.Parse(this.TextBoxEndDate.Text);
        }
        catch
        {
            LabelError.Text = "结束时间不合法！";
            return;
        }
        if (startdate > enddate)
        {
            LabelError.Text = "开始时间不能晚于结束时间！";
        }
        code = this.DrpProductTypeList.SelectedValue.ToString().Trim();
        this.GridViewSalesReport.DataSource = GetSalesReport(startdate, enddate, code);
        this.GridViewSalesReport.DataBind();
    }

    DataTable GetSalesReport(DateTime startdate, DateTime enddate, string productcode)
    {
        DataTable dt = new DataTable();

        using (System.Data.SqlClient.SqlConnection connect = new System.Data.SqlClient.SqlConnection(PublicDefine.SQLConnectString))
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append("select V_Products.ProductID,  V_Products.ProductCode,	V_Products.ProductName,  V_Products.ProductModule,V_Products.ProductColor,  Sum(SaleQuantity) as Quantity,  Sum(SaleSum) as SaleSum ");
            builder.Append(string.Format("from ( select * from view_Products where ProductCode like '{0}%' ) as V_Products ", productcode));
            builder.Append(" left join view_Orders on V_Products.ProductID = view_Orders.ProductID  ");
            builder.Append(string.Format(" and view_Orders.OrderDate >'{0}' and view_Orders.OrderDate < '{1}' ", startdate.ToString("yyyy-MM-dd"), enddate.ToString("yyyy-MM-dd")));
            builder.Append("group by V_Products.ProductID,V_Products.ProductCode,V_Products.ProductName,V_Products.ProductModule,V_Products.ProductColor  ");

            connect.Open();
            System.Data.SqlClient.SqlDataAdapter adpter = new System.Data.SqlClient.SqlDataAdapter(builder.ToString(),connect);
            adpter.Fill(dt);            
            connect.Close();
        }
        return dt;
    }
    protected void ButtonAddNewOrder_Click(object sender, EventArgs e)
    {
        Response.Redirect("NewSaleOrder.aspx");
    }
    protected void ButtonViewHistory_Click(object sender, EventArgs e)
    {
        Response.Redirect("OrderHistory.aspx");
    }
}
